package sjtu.ist.warehouse4ws.tool;




import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Domainmodelattr;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Functions;
import sjtu.ist.warehouse4ws.model.Parameter;
import sjtu.ist.warehouse4ws.model.Process;

public class ParserProcess {
	public String processId;

	public String parseNode(String tagName, Document doc){
		
        NodeList parentlist= doc.getElementsByTagName(tagName);      //获取该ID的标签
     
        Element head= (Element) parentlist.item(0);                 //获取该ID标签的第一个Item
       
        NodeList childlist=head.getChildNodes();                 //获取该ID标签第一个Item里面的结点
		if (childlist.getLength() != 0) {
			String tagvalue = childlist.item(0).getNodeValue(); // 取值
			if (tagvalue.isEmpty()) {
				return null;
			} else {
				return tagvalue;
			}
		} else {
			return null;
		}
		 
	}
	public Process reserveProcess(String path)  {
	
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		Process process = new Process();
		
		try {
			db = dbf.newDocumentBuilder();
			File file = new File(path);
			Document doc = db.parse(file);
			processId=parseNode("id",doc);
			String enName=parseNode("enName",doc);
			String cnName=parseNode("cnName",doc);
			String description =parseNode("description",doc);
			
			process.setId(processId);
			process.setEnName(enName);
			process.setCnName(cnName);
			process.setDescription(description);
			//process.setFileinfo(null);
			
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return process;
		
		
	}
	public ArrayList<Functions> reserveFunctions(String path) {
	
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		
		ArrayList<Functions> list=new ArrayList<Functions>();
			try {
				db = dbf.newDocumentBuilder();
				File file = new File(path);
				Document doc = null;
				doc = db.parse(file);
				Element root=doc.getDocumentElement();
				NodeList functionlist=root.getElementsByTagName("string");
				for(int i=0;i<functionlist.getLength();i++){
					Functions functions=new Functions();
					Element funId=(Element) functionlist.item(i);
					
					String funid=funId.getFirstChild().getNodeValue();
					//System.out.println(funid);
					functions.setFunctionid(funid);
					functions.setProcessid(processId);
					list.add(functions);
			}
				} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
			
				e.printStackTrace();
			}
	
			return list;

		
	}
	
	/*public String getNodeText(NodeList node){
		Element e=(Element)node.item(0);
		if(e==null){return null;}                        //this judge is very important ,without it ,you will found nullpoint Exception
		Text t=(Text)e.getFirstChild();
		if(t==null){return null;}
		String str=t.getNodeValue();
		return str;

	}*/

	
	public static void main(String[] args) throws IOException, Exception {
		
		ParserProcess test=new ParserProcess();
		String path="E:/runtime-EclipseApplication/demo2/功能视图/processes/11c4b74b-874c-4f1d-add8-ad012165a1d5.xml";
		Process p=test.reserveProcess(path);
		System.out.println(p.getCnName());
		System.out.println(p.getId());
		ArrayList<Functions> testfun=test.reserveFunctions(path);
		for(int i=0;i<testfun.size();i++){
			System.out.println(testfun.get(i).getFunctionid());
			System.out.println(testfun.get(i).getProcessid());
			
		}



	}

}
